package com.acme.service.bank;

import com.acme.domain.account.Account;
import com.acme.domain.account.AccountByBalanceComparator;
import com.acme.domain.account.AccountType;
import com.acme.domain.account.CheckingAccount;
import com.acme.domain.client.Bank;
import com.acme.domain.client.Client;

import java.math.BigDecimal;
import java.util.*;

public class BankReport {
    public int getNbOfCustomers(Bank bank) {
        return bank.getClients().size();
    }

    public int getNbOfAccounts(Bank bank) {
        int accountsCount = 0;
        for (Client client : bank.getClients()) {
            accountsCount += client.getAccounts().size();
        }
        return accountsCount;
    }

    public List<Account> getAccountsSortedBySum(Bank bank, final boolean ascending) {
        List<Account> accounts = new LinkedList<Account>();
        for (Client client : bank.getClients()) {
            accounts.addAll(client.getAccounts());
        }
        Collections.sort(accounts, new AccountByBalanceComparator(ascending));
        return accounts;
    }

    public BigDecimal getBankCredit(Bank bank) {
        BigDecimal credit = new BigDecimal(0);
        for (Client client : bank.getClients()) {
            for (Account account : client.getAccounts()) {
                if (account.getAccountType() == AccountType.CHECKING) {
                    credit = credit.add(((CheckingAccount) account).getCredit());
                }
            }
        }
        return credit;
    }

    public Map<Client, Collection<Account>> getCustomerAccount(Bank bank) {
        HashMap<Client, Collection<Account>> clientAccounts = new HashMap<Client, Collection<Account>>();
        for (Client client : bank.getClients()) {
            clientAccounts.put(client, client.getAccounts());
        }
        return clientAccounts;
    }
}